package com.scm.servlet.report;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.scm.model.Pomain;
import com.scm.model.Somain;
import com.scm.page.Page;
import com.scm.service.PomainService;
import com.scm.service.SomainService;

@WebServlet("/main/report/finance_detail")
public class FinanceReportDetailServlet extends HttpServlet{
	private static final Logger LOGGER = Logger.getLogger(FinanceReportDetailServlet.class);
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		//获取当前页码参数
		String currentPageStr = req.getParameter("currentPage");
		int currentPage = 1;//默认当前页码为第一页
		try{
			currentPage = Integer.parseInt(currentPageStr);
		}catch(NumberFormatException e){
			currentPage = 1;//参数不是数字或者没有该参数时，默认访问第一页
		}
		LOGGER.info("页码"+currentPage);
		String month = req.getParameter("month");
		Map<String, String> queryCondition = new HashMap<String, String>();
		queryCondition.put("type", "-1");
		queryCondition.put("startDate", month+"-01");
		queryCondition.put("endDate", month+"-31");
		
		try {
			Page<Pomain> page_po = new PomainService().selectPomain(queryCondition, currentPage);
			Page<Somain> page_so = new SomainService().selectSomain(queryCondition, currentPage);
			req.setAttribute("page_po", page_po);
			req.setAttribute("page_so",page_so);
			/*String json_po= JSONArray.toJSONString(page_po);//将数据转换成JSON格式的数据 (阿里巴巴提供的转成JSON格式的工具包)
			String json_so= JSONArray.toJSONString(page_so);//将数据转换成JSON格式的数据 (阿里巴巴提供的转成JSON格式的工具包)
			
			PrintWriter pw=resp.getWriter();
			pw.print(json_po);
			pw.print(json_so);
			pw.flush();
			pw.close();*/
			
			req.getRequestDispatcher("/main/report/finance_report_detail_layer.jsp").forward(req, resp);
		} catch (Exception e) {
			LOGGER.error("月度报表异常",e);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
